package com.lunhui.service.provider.controller;

import com.lunhui.cache.client.model.RoseCacheDto;
import com.lunhui.service.lunhui.service.business.CacheService;
import com.lunhui.service.lunhui.service.business.UserService;
import com.lunhui.service.lunhui.service.business.enums.ResultCodeEnum;
import com.lunhui.service.provider.common.Result;
import com.lunhui.service.provider.common.UserCacheAsp;
import com.lunhui.user.client.model.RoseDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author renminghe-kg
 */
@RestController
@RequestMapping("rose")
@Api(value = "角色接口")
public class LunHuiServiceRoseController {
    private static final Logger logger = LoggerFactory.getLogger(LunHuiServiceRoseController.class);

    @Autowired
    @Qualifier("cacheService")
    private CacheService cacheService;

    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @ApiOperation(value = "登录", httpMethod = "POST")
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public Result login(@ApiParam("角色Id") @RequestParam(name = "roseId") Integer roseId,
                        @ApiParam("用户id") @RequestParam(name = "userId") Integer userId,
                        @ApiParam("token") @RequestParam(name = "token") String token) {

        if (StringUtils.isEmpty(roseId) || StringUtils.isEmpty(token) || StringUtils.isEmpty(userId)) {
            return Result.fail(ResultCodeEnum.PARAMS_INVALID_CODE.getCode(),
                    ResultCodeEnum.PARAMS_INVALID_CODE.getName());
        }

        RoseDto roseDto;
        try {
            roseDto = userService.findRoseByRoseId(roseId);
        } catch (Exception e) {
            logger.error("获取角色信息失败: " + e);
            return Result.fail(ResultCodeEnum.EXCEPTION_CODE.getCode(),
                    ResultCodeEnum.EXCEPTION_CODE.getName());
        }

        if (!StringUtils.isEmpty(roseDto)) {
           try {
               RoseCacheDto roseCacheDto = new RoseCacheDto();
               BeanUtils.copyProperties(roseDto, roseCacheDto);

               cacheService.saveToDB(roseCacheDto);

               return Result.ok();
           } catch (Exception e) {
               logger.error("缓存角色信息失败: " + e);
               return Result.fail(ResultCodeEnum.EXCEPTION_CODE.getCode(),
                       ResultCodeEnum.EXCEPTION_CODE.getName());
           }
        }

        return null;
    }

    @ApiOperation(value = "获取角色当前位置", httpMethod = "GET")
    @RequestMapping(value = "location", method = RequestMethod.GET)
    @UserCacheAsp()
    @ResponseBody
    public Result getRoseCurrentLocation (@ApiParam("角色Id") @RequestParam(name = "roseId") Integer roseId,
                                          @ApiParam("用户id") @RequestParam(name = "userId") Integer userId,
                                          @ApiParam("token") @RequestParam(name = "token") String token) {

        logger.info("=========================");
        return null;

    }
}